<template>
  <page-container>
    <public-page-search
      @page-search-change="pageSearchChange"
      @onQuery="hnadlequeryBarStat"
    ></public-page-search>
    <police-list
      portionUrl="/aitype/caseInfo/queryCaseInfo"
      :params="queryForm"
      @change="handleList"
      @ready="handleReady"
      topicType="ct"
      :buttonList="buttonList"
      v-if="queryForm.callTimeStart"
      ref="listRef"
      :isList="true"
    />
  </page-container>
</template>
<script lang="ts">
import { defineComponent, reactive, ref, createVNode, onUnmounted } from 'vue';
import PublicPageSearch from '@/views/routine-analysis/list/components/public-page-search/index.vue';
import { Modal } from 'ant-design-vue';
import PoliceList from '../components/police-list/index.vue';
import { exportExcel } from '@/api/intelligentanalysis/alert';
import { downloadExcel, downloadForm } from '@/utils/download';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
//查询条件
interface queryForm {
  callTimeStart: string;
  callTimeEnd: string;
  inputParams?: string; //列表搜索内容
  callSummaryCodes?: string; //接警类别
  dealSummaryCodes?: string; //反馈类别
  xqdwCodes?: string; //辖区单位
  classifyCodes?: string;
  page: any;
  size: any;
  caseNos?: string;
  searchType: string; //传统和智能的切换
  inputType: string;
  dealFlag: any;
}
interface ButtonItem {
  type: string;
  name: string;
  background?: string;
  icon?: string;
  clickHandle: () => any;
}
export default defineComponent({
  components: {
    PublicPageSearch,
    PoliceList,
  },
  setup() {
    //查询字段
    let queryForm: queryForm = reactive({
      callTimeStart: '',
      callTimeEnd: '',
      page: 1,
      size: 50,
      searchType: '0',
      inputType: '',
      inputParams: '',
      dealFlag: '',
      callSummaryCodes: '',
      classifyCodes: '',
      dealSummaryCodes: '',
      xqdwCodes: '',
    });
    const caseNos = ref<any>([]);
    const handleList = value => {
      caseNos.value = value.toString();
      if (caseNos.value) {
        buttonList.value.map(item => {
          if (item.name == '导出全部') {
            item.name = '导出选中';
          }
        });
      } else {
        buttonList.value.map(item => {
          if (item.name == '导出选中') {
            item.name = '导出全部';
          }
        });
      }
    };
    const count = ref<number>(0);
    const handleReady = value => {
      count.value = value;
    };
    //导出
    const handleExlex = () => {
      if (caseNos.value.length) {
        let obj = Object.assign({ caseNos: caseNos.value }, queryForm);
        // exportExcel(obj).then(res => {
        //   downloadExcel(res, '警情列表');
        // });
        downloadForm((window as any).g.baseURL + '/aitype/caseInfo/exportExcel', obj);
      } else {
        if (count.value > 50000) {
          Modal.error({
            title: '提示',
            icon: createVNode(ExclamationCircleOutlined),
            content: '全部警情数量大于5万,请选择手动点击导出的数据!',
          });
        } else {
          Modal.confirm({
            title: '提示',
            icon: createVNode(ExclamationCircleOutlined),
            content: '是否导出全部数据？',
            okText: '确认',
            cancelText: '取消',
            onOk() {
              let obj = queryForm;
              obj.page = '';
              obj.size = '';
              // exportExcel(obj).then(res => {
              //   downloadExcel(res, '警情列表');
              // });
              downloadForm((window as any).g.baseURL + '/aitype/caseInfo/exportExcel', obj);
            },
          });
        }
      }
      return;
    };
    const buttonList = ref<Array<ButtonItem>>([
      {
        name: '导出全部',
        type: '',
        clickHandle: handleExlex,
      },
    ]);
    const flag = ref<boolean>(true);
    const pageSearchChange = params => {
      if (flag.value) {
        Object.keys(params).forEach(key => (queryForm[key] = params[key]));
      }
      flag.value = false;
    };
    const listRef = ref();
    const timer = ref<number | undefined>(undefined);
    const hnadlequeryBarStat = params => {
      Object.keys(params).forEach(key => (queryForm[key] = params[key]));
      timer.value = setTimeout(() => {
        listRef.value.getPoliceList();
        listRef.value.handlequerySummaryInfo();
      }, 1000);
    };
    onUnmounted(() => {
      clearTimeout(timer.value);
    });
    return {
      handleList,
      timer,
      queryForm,
      buttonList,
      pageSearchChange,
      count,
      handleReady,
      flag,
      hnadlequeryBarStat,
      listRef,
    };
  },
});
</script>
<style lang="less" scoped></style>
